<?php

	/**
	 * undocumented class
	 *
	 * @package default
	 * @author /bin/bash: niutil: command not found
	 **/
	class FundingsController extends AppController
	{
		var $name = 'Fundings';
		var $helpers = array('Html', 'Form', 'Javascript', 'Ajax');
		var $uses = array('Funding', 'Receiptbook', 'Project', 'ContractProfil');
		var $components = array('UserAuth', 'RequestHandler');

		function beforeFilter( ) {
		    if ($this->RequestHandler->isAjax()) {
		        $db =& ConnectionManager::getDataSource('default');
		        $db->fullDebug = false;
		    }
		}
		
		/**
		 * Add function if empty show add form
		 *
		 * @return void
		 * @author Rémi Peron
		 **/
		function add($project_id, $ticket_id)
		{
			if (empty($this->data)) {
				$this->Project->unbindModel(array('belongsTo' => array('Company'),
													'hasMany' => array('Batch', 'Document', 'Role')));
				$company = $this->Project->findById ($project_id);
				$cond = array(
						'company_id' => $company['Project']['company_id'],
						'project_id' => $project_id
						);
				$Receiptbook = $this->Receiptbook->generateList($cond, null,null, '{n}.Receiptbook.id', '{n}.Receiptbook.name');
				$autorized = $this->UserAuth->checkAcces($project_id, 'funding_add');
				
				// Plugin Gescom
				$this->set ('gescom', $company['Gescom']);
				$this->ContractProfil->unbindModel(array('belongsTo' => array('Contract')));
				$profils = $this->ContractProfil->findAllByContractId($company['Project']['contract_id']);
				$result = array();
				foreach ($profils as $profil) {
					$id = $profil['Profil']['id'];
					$result[$id] = $profil['Profil']['label'] . ' - '.   $profil['ContractProfil']['value'] . '€';
				}
				$this->set ('profils', $result);
				// End Plugin Gescom
				
				$this->set('disable', !$autorized);
				$this->set('Receiptbook', $Receiptbook);
				$this->set('project_id', $project_id);
				$this->set('ticket_id', $ticket_id);
				$this->render('addForm', 'ajax');
			} else {
				$this->data['Funding']['id'] = null;
				
				// Gescom plugin
				$this->Project->unbindModel(array('belongsTo' => array('Company'),
													'hasMany' => array('Batch', 'Document', 'Role')));
				$company = $this->Project->findById ($project_id);
				if ($company['Gescom'] != null) {
					$this->ContractProfil->unbindModel(array('belongsTo' => array('Contract')));
					$profils = $this->ContractProfil->findByProfilIdAndContractId( $this->data['Funding']['contract_profils_id'], $company['Project']['contract_id']);
					$this->data['Funding']['amount'] = $this->data['Funding']['days'] * $profils['ContractProfil']['value'];

				}
				// End Gescom
				
				if (!$this->Funding->save($this->data['Funding'])) {
					echo "Error";
				}
				$this->Receiptbook->id = $this->data['Funding']['receiptbook_id'];
				$rec = $this->Receiptbook->read();
//				pr ($rec);
				$rec['Receiptbook']['free_amount'] = $rec['Receiptbook']['free_amount'] - $this->data['Funding']['amount'];
				$this->Receiptbook->save($rec);
				$this->lists($project_id, $ticket_id);
			}
		}
		
		/**
		 * Lists all the fundings for a ticket
		 *
		 * @return void
		 * @author Rémi Peron
		 **/
		function lists($project_id, $ticket_id)
		{
			$this->Funding->unbindModel(array('belongsTo' => array('Receiptbook', 'Ticket')));
			$res = $this->Funding->findallByTicketId($ticket_id);
			$this->Project->unbindModel(array('belongsTo' => array('Company'),
												'hasMany' => array('Batch', 'Document', 'Role')));
			$company = $this->Project->findById ($project_id);
			$autorized = $this->UserAuth->checkAcces($project_id, 'funding_delete');
			$this->set('disable', !$autorized);
			// Plugin Gescom
			$this->set ('gescom', $company['Gescom']);
			$this->ContractProfil->unbindModel(array('belongsTo' => array('Contract')));
			$profils = $this->ContractProfil->findAllByContractId($company['Project']['contract_id']);
			$result = array();
			foreach ($profils as $profil) {
				$id = $profil['Profil']['id'];
				$result[$id] = $profil['Profil']['label'] . ' - '.   $profil['ContractProfil']['value'] . '€';
			}
			$this->set ('profils', $result);
			// End Plugin Gescom
			
			$cond = array(
					'company_id' => $company['Project']['company_id'],
					'project_id' => $project_id
					);
			$Receiptbook = $this->Receiptbook->generateList($cond, null,null, '{n}.Receiptbook.id', '{n}.Receiptbook.name');
			
			
			$this->set('Receiptbook', $Receiptbook);
			$this->set('fundings', $res);
			$this->set('project_id', $project_id);
			$this->render('lists', 'ajax');
		}
		
		/**
		 * update a Funding
		 *
		 * @return void
		 * @author Rémi Peron
		 **/
		function update()
		{
			$this->lists($project_id, $ticket_id);
			$this->render('lists', 'ajax');
		}
		
		/**
		 * Delete a funding
		 *
		 * @return void
		 * @author Rémi Peron
		 **/
		function delete($funding_id, $project_id, $ticket_id)
		{
			$autorized = $this->UserAuth->checkAcces($project_id, 'funding_add');
			if ($autorized) {
				$this->Funding->id = $funding_id;
				$fund = $this->Funding->read();
				$recei_it = $fund['Receiptbook']['id'];
				$amount = $fund['Funding']['amount'];
				$this->Funding->delete($funding_id, false);
				$this->lists($project_id, $ticket_id);
				$this->Receiptbook->id = $recei_it;
				$rec = $this->Receiptbook->read();
				$rec['Receiptbook']['free_amount'] = $rec['Receiptbook']['free_amount'] + $amount;
				$this->Receiptbook->save($rec);
			} else {
				$this->lists($project_id, $ticket_id);
			}

		}
		
	} // END class FundinsController extends AppController
?>